package com.itheima.controller.backend.Controller;

import com.itheima.DTO.CheckItemDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.controller.backend.Service.CheckItemService;
import com.itheima.pojo.CheckItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Api(tags ="检查项相关接口")
@Slf4j
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;


    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        log.info("新增检查项：{}",checkItem);
        checkItemService.addCheckItem(checkItem);

        return new Result(true,"新增检查项成功");
    }

    @PostMapping("/findPage")
    @ApiOperation("分页查询检查项")
    public PageResult findPage(@RequestBody CheckItemDTO checkItemDTO){
        log.info("分页查询检查项：{}",checkItemDTO);
        PageResult checkItemList = checkItemService.findPage(checkItemDTO, checkItemDTO.getCurrentPage(), checkItemDTO.getPageSize());
        return checkItemList;
    }

    @GetMapping("/delete")
    @ApiOperation("删除检查项")
    public Result deleteCheckItem(@RequestParam  Integer id){
        log.info("删除检查项：{}",id);
        checkItemService.deleteCheckItem(id);
        return new Result(true,"删除检查项成功");
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据检查组id查询关联的检查项id")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer checkgroupId){
        log.info("根据检查组id查询关联的检查项id：{}",checkgroupId);
        List<Integer> checkItemIds = checkItemService.findCheckItemIdsByCheckGroupId(checkgroupId);
        return new Result(true,"根据检查组id查询关联的检查项id成功",checkItemIds);
    }

    @PostMapping("/edit")
    @ApiOperation("编辑")
    @Transactional
    public Result edit(@RequestBody CheckItem checkItem){
        log.info("编辑检查项：{}",checkItem);
        checkItemService.edit(checkItem);
        return new Result(true,"编辑检查项成功");
    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        log.info("查询所有检查项");
        List<CheckItem> checkItemList = checkItemService.findAll();
        return new Result(true,"查询所有检查项成功",checkItemList);
    }

    @GetMapping("/findById")
    @ApiOperation("根据检查项id查询检查项")
    public Result findById(@RequestParam Integer id){
        log.info("根据检查项id查询检查项：{}",id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,"根据检查项id查询检查项成功",checkItem);
    }

}
